Skip to content

Support order_by in URL params of breakdown endpoints - #4502

Merged
apata merged 2 commits into
masterfrom
list-sort
Sep 2, 2024
Merged

apata merged 2 commits into
masterfrom
list-sort

Conversation

@apata

@apata apata commented Sep 2, 2024 •

Copy link
Copy Markdown
Contributor

Changes

  • Adds support for order_by in URL params of breakdown endpoints

Tests

  • Automated tests have been added

Changelog

  • Does not need a changelog entry

Documentation

  • This change does not need a documentation update

Dark mode

  • This PR does not change the UI

Comment on lines +502 to +525
test "shows sources for a page (using new filters)", %{conn: conn, site: site} do
populate_stats(site, [
build(:pageview, pathname: "/page1", referrer_source: "Google"),
build(:pageview, pathname: "/page1", referrer_source: "Google"),
build(:pageview,
user_id: 1,
pathname: "/page2",
referrer_source: "DuckDuckGo"
),
build(:pageview,
user_id: 1,
pathname: "/page1",
referrer_source: "DuckDuckGo"
)
])

filters = Jason.encode!([["is", "event:page", ["/page1"]]])
conn = get(conn, "/api/stats/#{site.domain}/sources?filters=#{filters}")

assert json_response(conn, 200)["results"] == [
%{"name" => "Google", "visitors" => 2},
%{"name" => "DuckDuckGo", "visitors" => 1}
]
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an additional sanity check for already existing functionality :)

@apata
apata force-pushed the list-sort branch 2 times, most recently from d609428 to 68ebb7a Compare September 2, 2024 07:39
end

defp put_order_by(query, %{} = params) do
order_by = Map.get(params, "order_by", nil) |> Filters.parse_order_by()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't clear Filters.parse_order_by is part of 'legacy' parsing code that we can delete soon and shouldn't be used outside of it.

Instead of calling Filters module we should either:
a) Make Plausible.Stats.Filters.QueryParser.parse_order_by public
or
b) Make it a defp in this class instead of in the filters module

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nil is also default when no value is found, so Map.get(params, "order_by") suffices, as well as params["order_by"]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions! Went with B, but got a warning on doc string (and therefore my doctests) being ignored on private functions, had to keep it def.

Comment thread lib/plausible/stats/filters/filters.ex Resolved
Comment thread lib/plausible/stats/breakdown.ex Resolved
Comment thread CHANGELOG.md Resolved
@apata
apata force-pushed the list-sort branch 2 times, most recently from e93f9a8 to 8581b1f Compare September 2, 2024 10:51

@macobo macobo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good (bar missing comment). 🥇

@apata
apata merged commit 9b59e95 into master Sep 2, 2024
@macobo
macobo deleted the list-sort branch September 2, 2024 12:11
@ruslandoga

ruslandoga commented Sep 10, 2024 •

Copy link
Copy Markdown
Contributor

👋 @apata

The new "order_by [[visit_duration, asc], [visit:source, desc]]] is respected and flipping the sort orders works" test fails CI in #4491: https://github.com/plausible/analytics/actions/runs/10785402818/job/29910533968

order_by_asc = Jason.encode!([["visit_duration", "asc"], ["visit:source", "desc"]])

conn1 =
get(conn, "/api/stats/#{site.domain}/sources?detailed=true&order_by=#{order_by_asc}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any time filtering happening here? If so, should the timestamps in pageviews be dynamic?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it filters the last month by default, since it started failing today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants